package MyDaily.dao.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import MyDaily.dao.DBconnection;
import MyDaily.pojo.User;

/*类说明:登录界面的查询方法*/

public class Login_Query {
	public static List<User> LoginQuery(String name, String password) {
		// 创建Connection
		Connection conn = null;
		// 创建PreparedStatement
		PreparedStatement pStat = null;
		// 创建ResultSet
		ResultSet rSet = null;
		List<User> userList = new ArrayList<User>();
		try {
			// 定义sql语句
			String sql = "SELECT `name`,`password` FROM `user` WHERE `name` = '" + name + "' AND `password` = '"
					+ password + "'";

			// 获取连接
			conn = DBconnection.getConnection();
			// 编译sql
			pStat = conn.prepareStatement(sql);
			// 执行sql，将查询结果放在结果集中
			rSet = pStat.executeQuery();
			// 判断是否有数据
			while (rSet.next()) {
				// 初始化实体类，并将查询结果添加user属性中
				User user = new User();
				user.setName(rSet.getString("name"));
				user.setPassword(rSet.getString("password"));
				// 将实体类放在userlist集合中
				userList.add(user);
			}
			return userList;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// 关闭资源
			try {
				if (pStat != null) {
					pStat.close();
				}

				if (conn != null) {
					conn.close();
				}
				if (rSet != null) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	// 查询检验
	public static void main(String[] args) {
		Login_Query querySoce = new Login_Query();
		querySoce.LoginQuery("林", "123");
		System.out.println(querySoce.LoginQuery("林", "123"));
	}
}
